package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appOrgDao")
public class AppOrgDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppOrg queryWithKeys(String appId, String orgId) throws SQLException {
		AppOrg dc = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_org WHERE app_id=? and ORGID=?");
			ps.setString(1, appId);
			ps.setString(2, orgId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new AppOrg();
				dc.setAppId(rs.getString("app_id"));
				dc.setOrgId(rs.getString("orgid"));
				dc.setOrganName(rs.getString("organname"));
				dc.setOrgLocate(rs.getString("org_locate"));

			}
		} catch (SQLException e) {
			throw new SQLException("queryAppTradePrivateWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return dc;
	}

	/**
	 * 根插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int saveAppOrg(AppOrg appOrg) throws SQLException {

		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into app_org values(?,?,?,?);";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appOrg.getAppId());
			ps.setString(2, appOrg.getOrgId());
			ps.setString(3, appOrg.getOrganName());
			ps.setString(4, appOrg.getOrgLocate());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	/**
	 * 修改模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int update(AppOrg appOrg) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update  app_org  set organname=?,org_locate=? where app_id=? and orgid=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appOrg.getOrganName());
			ps.setString(2, appOrg.getOrgLocate());
			ps.setString(3, appOrg.getAppId());
			ps.setString(4, appOrg.getOrgId());

			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(AppOrg appOrg) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from app_org where app_id=? and orgid=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appOrg.getAppId());
			ps.setString(2, appOrg.getOrgId());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
